package com.resou.reader.mine.vipcenter.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.api.entry.CarousePicBean;
import com.resou.reader.api.entry.MonthlyVipBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.resou.reader.R;
import com.resou.reader.api.entry.PrivilegeBean;
import com.resou.reader.api.entry.UserInfoBean;
import com.resou.reader.assist.WebActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.choosegender.giftbook.GridSpacingItemDecoration;
import com.resou.reader.data.bookstore.model.BasicList;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.mine.vipcenter.MonthlyVipActivity;
import com.resou.reader.mine.vipcenter.callback.IMonthlyVipCallbackListener;
import com.resou.reader.utils.AutoHeightLayoutManager;
import com.resou.reader.utils.DateUtils;
import com.resou.reader.utils.TextUtils;
import com.resou.reader.view.CarouselRecyclerView;
import com.resou.reader.view.CircleImageView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;


/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 包月VIP 适配器容器
 * CreateTime: 2019-04 -15
 **/
public class MonthlyVipAdapter extends BaseMultiItemQuickAdapter<MonthlyVipBean, BaseViewHolder> {


    public MonthlyVipAdapter(@Nullable List<MonthlyVipBean> data) {
        super(data);
        addItemType(MonthlyVipBean.HEADER, R.layout.adapter_monthlyvip_header);
        addItemType(MonthlyVipBean.PRIVILEGE, R.layout.adapter_monthlyvip_privilege);
        addItemType(MonthlyVipBean.UPDATAGRID, R.layout.adapter_monthlyvip_updata);
        addItemType(MonthlyVipBean.RECOMMENDLIST, R.layout.adapter_monthlyvip_booklist);
        addItemType(MonthlyVipBean.RECOMMENDGRID, R.layout.adapter_monthlyvip_bookinfo_list_grid);
    }

    @Override
    protected void convert(BaseViewHolder helper, MonthlyVipBean item) {
        switch (helper.getItemViewType()) {
            case MonthlyVipBean.HEADER:
                setBanner(helper, item);
                break;
            case MonthlyVipBean.PRIVILEGE:
                setPrivilege(helper, item);
                break;
            case MonthlyVipBean.UPDATAGRID:
                setUpdata(helper, item);
                break;
            case MonthlyVipBean.RECOMMENDLIST:
                setbookList(helper, item);
                break;
            case MonthlyVipBean.RECOMMENDGRID:
                setGrid_List(helper, item);
                break;

        }
    }


    /**
     * 设置banner图
     *
     * @param helper
     * @param item
     */
    private synchronized void setBanner(BaseViewHolder helper, MonthlyVipBean item) {
        List<CarousePicBean> carousePicBeanList = item.getList();
        if (carousePicBeanList == null) {
            return;
        }
        Banner mBanner = helper.getView(R.id.mBanner);
        List<CarousePicBean> banners = item.getList();

        mBanner.setImages(banners);
        mBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                CarousePicBean carousePicBean = (CarousePicBean) path;
                Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(carousePicBean.getIndexPic() + "").into(imageView);
            }
        });
        mBanner.setOnBannerListener(position1 -> {
            int type = banners.get(position1).getType();
            CarousePicBean picBean = banners.get(position1);
            if (type == 1) {
                BookDetailActivity.startDetailActivity(mContext, banners.get(position1).getContent());
            }
            if (type == 2) {
                WebActivity.startActivity(mContext, picBean.getContent(), picBean.getNovelName());
            }
            if (type == 3) {
                MonthlyVipActivity.start(mContext);
            }
        });
        mBanner.start();
        TextView tvOpening = helper.getView(R.id.tvOpening);
        tvOpening.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iMonthlyVipCallbackListener != null) {
                    iMonthlyVipCallbackListener.setIMonthlyVipOpeningVip();
                }
            }
        });
        UserInfoBean userInfoBean = item.getUserInfoBean();
        if (userInfoBean == null || userInfoBean.getU() == null) {
            return;
        }
        Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(userInfoBean.getU().getHead_pic() + "").into((CircleImageView) helper.getView(R.id.mCircleImageView));
        helper.setText(R.id.tvNickName, userInfoBean.getU().getNickName() + "");
        ImageView ivVipIcon = helper.getView(R.id.ivVipIcon);
        TextView tvVIPInfo = helper.getView(R.id.tvVIPInfo);
        ImageView ivBg = helper.getView(R.id.ivBg);
        if (userInfoBean.getU().isVipUser() == 1) {
            tvOpening.setBackgroundResource(R.drawable.shap_vipleftradius2);
            tvOpening.setText("立即续费");
            ivVipIcon.setVisibility(View.VISIBLE);
            ivVipIcon.setImageResource(R.mipmap.ic_vip_iconed);
            int day = DateUtils.getDateDay2((userInfoBean.getU().getVipEndTime()));
            TextUtils.setTimeColor(tvVIPInfo, "包月VIP特权中，剩余" + day + "天", day + "");
            ivBg.setBackgroundResource(R.mipmap.ic_monthly_headercard);
            if (new Date().getTime() > Long.valueOf(userInfoBean.getU().getVipEndTime())) {
                ivVipIcon.setImageResource(R.mipmap.ic_vip_icon);
                tvVIPInfo.setText("您的包月VIP已过期");
                TextUtils.setTimeColor(tvVIPInfo, "您的包月VIP已过期", "已过期");
            }
        } else {
            tvOpening.setBackgroundResource(R.drawable.shap_vipleftradius);
            tvOpening.setText("立即开通");
            ivVipIcon.setVisibility(View.GONE);
            tvVIPInfo.setText("未开通包月VIP");
            TextUtils.setTimeColor(tvVIPInfo, "开通包月，海量好书，免费阅读", "免费");
            ivBg.setBackgroundResource(R.mipmap.ic_monthly_header_pic_no);
        }
    }

    PrivilegeAdapter privilegeAdapter = null;

    /**
     * 设置特权信息
     *
     * @param helper
     * @param item
     */
    private synchronized void setPrivilege(BaseViewHolder helper, MonthlyVipBean item) {
        PrivilegeBean privilegeBean = item.getPrivilegeList();
        if (privilegeBean != null) {
            ImageView ivVIPPic = helper.getView(R.id.ivVIPPic);
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(privilegeBean.getVipBookListEntry() + "").into(ivVIPPic);
            RecyclerView mRecyclerView = helper.getView(R.id.mRecyclerView);
            AutoHeightLayoutManager linearLayoutManager = new AutoHeightLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerView.setLayoutManager(linearLayoutManager);
            privilegeAdapter = new PrivilegeAdapter(R.layout.privilege_adapter, privilegeBean.getPrivilegeDetail());
            mRecyclerView.setAdapter(privilegeAdapter);

            ivVIPPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iMonthlyVipCallbackListener != null) {
                        iMonthlyVipCallbackListener.setIMonthlyVipMonthlySub();
                    }
                }
            });
        }
    }

    /**
     * 小编推荐视图
     *
     * @param helper
     * @param item
     */
    private synchronized void setbookList(BaseViewHolder helper, MonthlyVipBean item) {
        BasicList<PlateNovels> basicList = item.getPlateNovelList();
        if (basicList != null) {
            helper.setText(R.id.tvTitle, basicList.getPlateName())
                    .setText(R.id.tvDes, basicList.getPlateSubName() + "");
            CarouselRecyclerView mRecyclerView = helper.getView(R.id.mRecyclerView);
            mRecyclerView.setLayoutManager(new AutoHeightLayoutManager(mContext));
            MonthlyVipListAdapter monthlyVipListAdapter = new MonthlyVipListAdapter(R.layout.adapter_monthlyvip_bookinfo_list, item.getPlateNovelList().getPlateNovels());
            mRecyclerView.setAdapter(monthlyVipListAdapter);
            TextView tvMore = helper.getView(R.id.tvMore);
            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iMonthlyVipCallbackListener != null) {
                        iMonthlyVipCallbackListener.setIMonthlyVipMoreCallbackListener(basicList.getPlateName(), basicList.getPlateId(), basicList.getPlateViewType(), helper.getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 换一换
     *
     * @param helper
     * @param item
     */
    private synchronized void setUpdata(BaseViewHolder helper, MonthlyVipBean item) {
        BasicList<PlateNovels> basicList = item.getPlateNovelList();
        if (basicList != null) {
            helper.setText(R.id.tvTitle, basicList.getPlateName())
                    .setText(R.id.tvDes, basicList.getPlateSubName() + "");
            CarouselRecyclerView mRecyclerView = helper.getView(R.id.mRecyclerView);
            if (!item.isRemove()) {
                mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, basicList.getPlateShowCount()));
                mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(basicList.getPlateShowCount(), 15, false));
                MonthlyVipGridAdapter monthlyVipGridAdapter = new MonthlyVipGridAdapter(R.layout.adapter_monthlyvip_bookinfo_grid, item.getPlateNovelList().getPlateNovels());
                monthlyVipGridAdapter.openLoadAnimation(SCALEIN);
                mRecyclerView.setAdapter(monthlyVipGridAdapter);
                item.setRemove(true);
                TextView tvMore = helper.getView(R.id.tvMore);
                tvMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (iMonthlyVipCallbackListener != null) {
                            iMonthlyVipCallbackListener.setIMonthlyVipRefreshCallbackListener(monthlyVipGridAdapter, basicList.getPlateId(), basicList.getPlateViewType(), helper.getAdapterPosition());
                        }
                    }
                });
            }
        }
    }

    PlateNovels plateNovels;

    /**
     * 都市-玄幻-等列表信息
     *
     * @param helper
     * @param item
     */
    private synchronized void setGrid_List(BaseViewHolder helper, MonthlyVipBean item) {
        BasicList<PlateNovels> basicList = item.getPlateNovelList();
        if (basicList.getPlateNovels().size() == 0) {
            return;
        }
        TextView tvMore = helper.getView(R.id.tvMore);
        tvMore.setTag(basicList.getPlateName());
        if (basicList != null) {
            helper.setText(R.id.tvTitle, basicList.getPlateName())
                    .setText(R.id.tvDes, basicList.getPlateSubName() + "");
            CarouselRecyclerView mRecyclerView = helper.getView(R.id.mRecyclerView);
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
            basicList.getPlateNovels().get(0).setType(1);
            MonthlyVipGrid_ListAdapter monthlyVipGrid_listAdapter = new MonthlyVipGrid_ListAdapter(basicList.getPlateNovels());
            mRecyclerView.setAdapter(monthlyVipGrid_listAdapter);
            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iMonthlyVipCallbackListener != null) {
                        iMonthlyVipCallbackListener.setIMonthlyVipMoreCallbackListener(basicList.getPlateName(), basicList.getPlateId(), basicList.getPlateViewType(), helper.getAdapterPosition());
                    }
                }
            });
//            LinearLayout llBookInfo = helper.getView(R.id.llBookInfo);
//            llBookInfo.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    BookDetailActivity.startDetailActivity(mContext, plateNovels.getNovelId());
//                }
//            });
        }
    }

    public IMonthlyVipCallbackListener iMonthlyVipCallbackListener;

    /**
     * 详情点击监听
     *
     * @param iMonthlyVipCallbackListener
     */
    public void setiMonthlyVipCallbackListener(IMonthlyVipCallbackListener iMonthlyVipCallbackListener) {
        this.iMonthlyVipCallbackListener = iMonthlyVipCallbackListener;
    }


}

